﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class CellAlarm_ReasonRpt : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Dep_Bind();
            Auth();
            SetPager();
        }
    }

    private void SetPager()
    {
        PagedDataSource pds = new PagedDataSource();
        DataSet ds = GetReason();
        ReasonPager.RecordCount = ds.Tables[0].Rows.Count;
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = ReasonPager.PageSize;
        pds.CurrentPageIndex = ReasonPager.CurrentPageIndex - 1;
        ReasonList.DataSource = pds;
        ReasonList.DataBind();
    }

    private DataSet GetReason()
    {
        string sqlstr = @"select cell_chname,start_time,recover_time,cell_class,main_reason,IsTele,Remark,rid,CELL_NET
                                    ,second_reason,cell_net from U_UNLINK_REASON_T WHERE 1=1 ";
        if (Dep_A.SelectedValue != "0")
        {
            sqlstr += " and CELL_CHNAME like '" + Dep_A.SelectedValue + "%'";
        }
        if (Cell_CHName_A.Text.Trim().Length > 0)
        {
            sqlstr += " and CELL_CHNAME like '%" + Tools.ReplaceString(Cell_CHName_A.Text.Trim(), 50) + "%'";
        }
        if (Main_Reason.SelectedValue.Length > 0)
        {
            sqlstr += " and main_reason = '" + Main_Reason.SelectedValue + "'";
        }
        sqlstr += " order by start_time desc";
        return OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, null);
    }

    private void Dep_Bind()
    {
        string sqlstr = "SELECT DISTINCT SUBSTR(Replace(CELL_CHNAME,'G',''),0,2) AS County FROM CELL_CLASS ORDER BY SUBSTR(Replace(CELL_CHNAME,'G',''),0,2)";
        Dep_A.DataSource = OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, null).Tables[0].DefaultView;
        Dep_A.DataTextField = "County";
        Dep_A.DataValueField = "County";
        Dep_A.DataBind();
        Dep_A.Items.Insert(0, new ListItem("全市", "0"));
    }

    private void Auth()
    {
        DataRow dr = GetOrganAndCompany().Rows[0];
        if ((!dr["company"].ToString().Equals("延安")) || dr["organ"].ToString().Equals("宝塔维护站"))
        {
            Dep_A.SelectedValue = dr["company"].ToString();
        }
        else
        {
            Dep_A.Enabled = true;
        }
    }

    protected void QueryReason_Click(object sender, EventArgs e)
    {
        SetPager();
    }

    protected void ReasonPager_PageChanged(object sender, EventArgs e)
    {
        SetPager();
    }
}
